"use client";

import type { AgentSummary, PresenceState } from "@agent-society/types";
import clsx from "clsx";

const PRESENCE_COLORS: Record<PresenceState, string> = {
  online: "bg-emerald-400",
  away: "bg-amber-400",
  offline: "bg-slate-600"
};

export interface AgentNavigatorProps {
  agents: AgentSummary[];
  selected?: string;
  onSelect?: (id: string) => void;
}

export function AgentNavigator({ agents, selected, onSelect }: AgentNavigatorProps) {
  return (
    <aside className="surface-panel flex h-full flex-col gap-3 p-4">
      <header>
        <h2 className="text-lg font-semibold text-slate-100">Agents</h2>
        <p className="text-sm text-slate-400">Live status and balances</p>
      </header>
      <div className="flex-1 space-y-2 overflow-y-auto pr-1">
        {agents.map(agent => {
          const presenceState: PresenceState = agent.presence?.state ?? "offline";
          const presenceColor = PRESENCE_COLORS[presenceState] ?? PRESENCE_COLORS.offline;
          const presenceClient = agent.presence?.client;

          return (
            <button
              key={agent.id}
              id={`agent-${agent.id}`}
              type="button"
              onClick={() => onSelect?.(agent.id)}
              className={clsx(
                "w-full rounded-lg border px-3 py-2 text-left transition",
                selected === agent.id
                  ? "border-sky-400/60 bg-sky-500/10 text-slate-100"
                  : "border-slate-700/40 bg-slate-900/40 text-slate-200 hover:border-slate-500/50"
              )}
            >
              <div className="flex items-center justify-between">
                <span className="font-medium">{agent.name}</span>
                <span className="text-sm text-slate-400">{agent.balance} cr</span>
              </div>
              <div className="mt-1 flex items-center gap-2 text-xs text-slate-400">
                <span className={clsx("h-2 w-2 rounded-full", presenceColor)} aria-hidden />
                <span className="capitalize">{presenceState}</span>
                <span className="text-slate-600">•</span>
                <span>{agent.role}</span>
                {presenceClient ? (
                  <span className="ml-auto text-slate-500">{presenceClient}</span>
                ) : null}
              </div>
            </button>
          );
        })}
      </div>
    </aside>
  );
}
